﻿using Outlook = Microsoft.Office.Interop.Outlook;

namespace OutlookMessageWrapper
{
    /// <summary>
    /// Creates the object type of one of the message type class based on the message type
    /// </summary>
    public class OutlookMessageItemFactory
    {
        /// <summary>
        /// Returns the object of outlook item wrapper corresponding to outlook item
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static OutlookMessageItemWrapper GetMessageItem(object message)
        {
            OutlookMessageItemWrapper messageWrapper = null;

            if (message is Outlook.MailItem)
            {
                // Mail item
                Outlook.MailItem olMail = message as Outlook.MailItem;
                messageWrapper = new OutlookMailItem(olMail);
            }
            else if (message is Outlook.AppointmentItem)
            {
                // Appointment item
                Outlook.AppointmentItem olAppointment = message as Outlook.AppointmentItem;
                messageWrapper = new OutlookAppointmentItem(olAppointment);
            }
            else if (message is Outlook.TaskItem)
            {
                // Task item
                Outlook.TaskItem olTask = message as Outlook.TaskItem;
                messageWrapper = new OutlookTaskItem(olTask);
            }
            else if (message is Outlook.PostItem)
            {
                // Post item
                Outlook.PostItem olPost = message as Outlook.PostItem;
                messageWrapper = new OutlookPostItem(olPost);
            }
            else if (message is Outlook.ContactItem)
            {
                // Contact item
                Outlook.ContactItem olContact = message as Outlook.ContactItem;
                messageWrapper = new OutlookContactItem(olContact);
            }
            else if (message is Outlook.DocumentItem)
            {
                // Document item
                Outlook.DocumentItem olDocument = message as Outlook.DocumentItem;
                messageWrapper = new OutlookDocumentItem(olDocument);
            }
            else 
            {
                // Unsupported format
                // If you want you can write wrapper for new item type
            }

            return messageWrapper;
        }
    }
}
